class ProfileController < ApplicationController

layout "common_layout"
  def amend
   # @user = User.find_all_by_account(session[:user_id])
    case session[:user_type]
      when "student"
        redirect_to( :action => "student_amend")
      when "teacher"
        redirect_to( :action => "teacher_amend")
      when "administrator"
        redirect_to(:action=>"admin_amend")
    end
  end
  def admin_amend
    @admin=User.find_by_account(session[:account])
  end
  def student_amend
    @student = User.find_by_account(session[:account])
    
     if request.post?  
       @student.MSN = params[:student][:MSN] 
       @student.email = params[:student][:email] 
       @student.sex = params[:student][:sex] 
       @student.tel = params[:student][:tel] 
      if @student.save
         flash.now[:notice] = "Change Profile Successfully!"
      else
         flash.now[:notice] = "Have not Changed Your Profile !"
      end
     end
  end
  
  def teacher_amend
    @teacher = User.find_by_account(session[:account])
    
     if request.post?
       @teacher.MSN = params[:teacher][:MSN] 
       @teacher.email = params[:teacher][:email] 
       @teacher.tel = params[:teacher][:tel]

      if  @teacher.save
         flash.now[:notice] = "Change Profile Successfully!"
      else
         flash.now[:notice] = "Have not Changed Your Profile !"
      end
    end
  end
  
  
  def result 
    
  end
  def change_password
     @user=nil
    if request.post? 
        if params[:user][:newpassword] == params[:user][:newpassword_confirmation]
           @user=User.change_password(session[:account],session[:user_type],params[:user][:password],params[:user][:newpassword])
        end
        if @user and @user.save
            flash[:notice] = "Change password successfully!"
          else
            flash[:notice] = "Have not Changed password !"
          end
    end
    if session[:user_type]=="student"
      redirect_to(:action => "student_amend")
    elsif session[:user_type]=="teacher"
      redirect_to(:action=>"teacher_amend")
    elsif session[:user_type]=="administrator"
      redirect_to(:action=>"admin_amend")
    end
  end
end
